package com.sftz.framework.service.aliyun;

import java.util.List;
import java.util.Map;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetMezzanineInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetMezzanineInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetMezzanineInfoResponse.Mezzanine;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse.Video;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.sftz.framework.rersult.MobileResult;
import com.sftz.framework.service.aliyun.config.AliyunAkConfig;
import com.uxuexi.core.common.util.Util;

/**
 * 阿里云视频点播,播放工具，用于获取视频的播放凭证或者播放地址
 * <p>
 * 播放器SDK根据播放凭证会自动获取播放地址进行解码播放，播放地址的时效为30分钟，若失效请重新获取播放凭证回传给播放器SDK用于刷新播放地址。
 * @author   朱晓川
 * @Date	 2017年11月22日
 */
public class AliyunVodUtil {

	private static final Log log = Logs.get();

	private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", AliyunAkConfig.ACCESS_KEY_ID,
			AliyunAkConfig.ACCESS_KEY_SECRET);

	private AliyunVodUtil() {
	}

	/**
	 * 获取视频的播放凭证
	 * <p>
	 * 注意：播放凭证时效为100秒，只能用于获取指定视频的播放地址，不能混用或重复使用；如果凭证过期则无法获取播放地址，请重新获取凭证
	 * @param vid 视频id,不能为空
	 */
	public static String getVideoPlayAuth(String vid) {

		if (Util.isEmpty(vid)) {
			throw new IllegalArgumentException("视频id不能为空");
		}

		DefaultAcsClient client = new DefaultAcsClient(profile);
		GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
		request.setVideoId(vid);
		GetVideoPlayAuthResponse response = null;
		String playAuth = ""; //播放凭证
		try {
			response = client.getAcsResponse(request);
			playAuth = response.getPlayAuth();
		} catch (ServerException e) {
			e.printStackTrace();
			log.error("GetVideoPlayAuthRequest Server failed");
		} catch (ClientException e) {
			e.printStackTrace();
			log.error("GetVideoPlayAuthRequest Client failed");
		}

		return playAuth;
	}

	/**
	 * 获取视频信息
	 * <p>
	 * 正常调用返回视频对象，否则返回null
	 * @param vid 视频vid
	 */
	public static Video getVideoInfo(String vid) {
		if (Util.isEmpty(vid)) {
			throw new IllegalArgumentException("视频id不能为空");
		}

		DefaultAcsClient client = new DefaultAcsClient(profile);
		GetVideoInfoRequest request = new GetVideoInfoRequest();
		request.setVideoId(vid);

		GetVideoInfoResponse response = new GetVideoInfoResponse();

		try {
			response = client.getAcsResponse(request);
			return response.getVideo();
		} catch (ServerException e) {
			e.printStackTrace();
			log.error("GetVideoInfoRequest Server failed");
		} catch (ClientException e) {
			e.printStackTrace();
			log.error("GetVideoInfoRequest Client failed");
		}
		return null;
	}

	/**
	 * 获取视频播放信息
	 */
	public static GetPlayInfoResponse getVideoInfos(String videoId) {
		GetPlayInfoRequest request = new GetPlayInfoRequest();
		request.setVideoId(videoId);
		GetPlayInfoResponse response = null;
		try {
			DefaultAcsClient client = new DefaultAcsClient(profile);
			response = client.getAcsResponse(request);
		} catch (ServerException e) {
			throw new RuntimeException("GetPlayInfoRequest Server failed");
		} catch (ClientException e) {
			throw new RuntimeException("GetPlayInfoRequest Client failed");
		}
		return response;
	}

	/**
	 * 删除视频
	 * <p>
	 * 支持删除多个(逗号分割)
	 * @param vid 视频vid列表
	 */
	public static Map<String, Object> deleteVideo(String vids) {
		if (Util.isEmpty(vids)) {
			throw new IllegalArgumentException("视频id列表不能为空");
		}

		DefaultAcsClient client = new DefaultAcsClient(profile);
		DeleteVideoRequest request = new DeleteVideoRequest();
		request.setVideoIds(vids);

		try {
			DeleteVideoResponse resp = client.getAcsResponse(request);
			return MobileResult.success("success", resp);
		} catch (ServerException e) {
			log.error("GetVideoInfoRequest Server failed:" + e.getMessage());
			e.printStackTrace();
			return MobileResult.error(e.getMessage(), null);
		} catch (ClientException e) {
			log.error("GetVideoInfoRequest Client failed:" + e.getMessage());
			e.printStackTrace();
			return MobileResult.error(e.getMessage(), null);
		}
	}

	/**
	 * 获取媒体资源信息
	 * 
	 * <p>
	 * @param vid 视频ID
	 */
	public static Mezzanine getMezzanine(String vid) {
		if (Util.isEmpty(vid)) {
			throw new IllegalArgumentException("视频id不能为空");
		}
		DefaultAcsClient client = new DefaultAcsClient(profile);

		GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
		request.setVideoId(vid);

		try {
			GetMezzanineInfoResponse resp = client.getAcsResponse(request);
			Mezzanine mezzanine = resp.getMezzanine();
			return mezzanine;
		} catch (ServerException e) {
			log.error("GetMezzanineInfoRequest Server failed:" + e.getMessage());
			e.printStackTrace();
		} catch (ClientException e) {
			log.error("GetMezzanineInfoRequest Client failed:" + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		// 传入视频ID 
		GetPlayInfoResponse response = getVideoInfos("0a82a703273c40ec947a6ee6f5fab772");
		List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
		// 播放地址
		for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
			System.out.println(playInfo.getPreprocessStatus() + "播放时长：" + playInfo.getStreamType());
			System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL());
		}
	}
}